package com.cwt_taxi_xa.protocol.card.cmd;

import android.text.TextUtils;

import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.Tools;

import java.io.UnsupportedEncodingException;
/**
 * 服务监督卡
 * @author Administrator
 * @Deprecated
 */
public class CardSupervisionEF0B extends BaseCmd<AckCallBack>{

	final String TAG = "CardSupervisionEF0B";
	public static int EF0B_index = 1;
	private String carn;
	public CardSupervisionEF0B(String cmdTag) {
		super(cmdTag);
		// TODO Auto-generated constructor stub
	}



	public CardSupervisionEF0B(String cmdTag, AckCallBack ackCallBack,String carn) {
		super(cmdTag, ackCallBack);
		this.carn = carn;
		// TODO Auto-generated constructor stub
	}



	@Override
	public String data() {
		// TODO Auto-generated method stub
		EF0B_index = 1;
		return "02 EF0B 04 0001 00C8";//04 循环记录文件 /0001 索引/00C8是两个字节
	}



	@Override
	public void decode(byte[] order) {
		// TODO Auto-generated method stub
		byte[] data1 = substringSecondLastNull(order);
		AppLog.e(Tools.parseByte2HexStr2(data1));
		try {
			String str = new String(data1, "GBK");

			String[] ss = str.split(",");
			AppLog.d("----------------------------------------------------------");
			AppLog.d("数量："+ss.length);
			AppLog.d("----------------------------------------------------------");
			for (int i = 0; i < ss.length; i++) {
				AppLog.e(" "+(i+1)+": "+ss[i]);
			}
			AppLog.e("----------------------------------------------------------");

			if(ss.length>1){
				//循环记录不足时，读取报6A83错误
				AppLog.e( TAG +" carn:" + carn + "--- ss[1]:" + trim(ss[1]) + " endsWith:" + carn.endsWith(trim(ss[1])));
				//西交投写卡加空格了
				if((!defaultNumber(carn) && !carn.endsWith(trim(ss[1])))
				/*|| (!defaultNumber(carn) && !trim(ss[1]).endsWith(carn))*/){
					EF0B_index++;
					if(EF0B_index <= 9){
						ackCallBack.sendCmd(0x0021, "02 EF0B 04 000"+ EF0B_index + " 00C8");
					}else if(EF0B_index <= 10){
						String EF0Bindex = Tools.parseByte2HexStr(Tools.int2Bytes2(EF0B_index, 2));
						AppLog.e(TAG," EF0Bindex:" + EF0Bindex);
						ackCallBack.sendCmd(0x0021, "02 EF0B 04 "+ EF0Bindex +" 00C8");
					}else{
						//没有相同的车
						ackCallBack.error(cmdTag,0,"车辆信息不符,非准驾驶车辆,无法签到签退");
					}
					return;
				}else{
					//找到对应车牌
					ackCallBack.callBack(cmdTag, ss);
				}
				//添加
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			AppLog.e(TAG, e.getMessage());
			ackCallBack.error(cmdTag,1,"服务监督卡编码错误");
		}
		//AppLog.d(TAG,"开始读取二维码");

		//AppLog.d(TAG,"卡片信息异常");

	}
	private String trim(String value){
		if(!TextUtils.isEmpty(value)){
			return value.replaceAll("\\s*", "");
		}
		return "";
	}

}
